--准备阶段，你可以声明一个数，亮出牌堆顶四张牌并获得其中点数大于声明值的牌，若此做
--本回合你使用牌结算后，若你本回合使用牌点数均值大于声明值，你横置或结束此回合

local duohang = fk.CreateSkill{
  name = "doublez__duohang",
}

local U = require "packages.utility.utility"

Fk:loadTranslationTable{
  ["doublez__duohang"] = "度航",
  [":doublez__duohang"] = "准备阶段，你可以声明一个数，亮出牌堆顶四张牌并获得其中点数大于声明值的牌，若此做"..
    "本回合你使用牌结算后，若你本回合使用牌点数均值大于声明值，你横置或结束此回合",

  ["@doublez__duohang-turn"] = "度航",
  ["doublez__duohang_endTurn"] = "结束回合",

  ["@doublez__duohang_mean-turn-inhand"] = "均值变为",
  
  ["$doublez__duohang1"] = "宦海沉浮，生死难料!",
  ["$doublez__duohang2"] = "跨海南征，波涛起浮。",
}

duohang:addEffect(fk.EventPhaseStart, {
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(self) and target == player and player.phase == Player.Start
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local number
    local choices = {}
    for i = 1, 13 do
      table.insert(choices, tostring(i))
    end
    number = tonumber(room:askToChoice(player, {choices = choices, skill_name = duohang.name})) ---@type integer
    room:setPlayerMark(player, "@doublez__duohang-turn", number)
    local cards = room:getNCards(4)
    room:moveCardTo(cards, Card.Processing, nil, fk.ReasonJustMove, duohang.name)
    room:delay(500)
    local aquire_cards = table.filter(cards, function (cid)
      return Fk:getCardById(cid).number > number
    end)
    room:obtainCard(player, aquire_cards, false, fk.ReasonPrey, player, duohang.name)
    room:delay(200)
    room:cleanProcessingArea(cards, duohang.name)
    room:delay(200)
  end
})

local reset = function (self, event, target, player, data)
  local room = player.room
  local sum, num = 0, 0;
  room.logic:getEventsOfScope(GameEvent.UseCard, 1, function (e)
    sum = sum + e.data.card.number
    num = num + 1
  end, Player.HistoryTurn)
  table.forEach(player:getCardIds("h"), function (cid)
    local c = Fk:getCardById(cid)
    room:setCardMark(c, "@doublez__duohang_mean-turn-inhand", math.floor((sum + c.number)*10/(num + 1))/10)
  end)
end


duohang:addEffect(fk.AfterCardsMove, {
  can_refresh = function (self, event, target, player, data)
    if player.room.current ~= player or player:usedSkillTimes(duohang.name) == 0  then return end
    for _, move in ipairs(data) do
      if move.to == player and move.toArea == Player.Hand then
        return true
      end
    end
  end,
  on_refresh = reset,
})

duohang:addEffect(fk.CardUseFinished, {
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    if player:usedSkillTimes(duohang.name) > 0 and target == player then
      local room = player.room
      local sum, num = 0, 0;
      room.logic:getEventsOfScope(GameEvent.UseCard, 1, function (e)
        sum = sum + e.data.card.number
        num = num + 1
      end, Player.HistoryTurn)
      return (sum/num) > player:getMark("@doublez__duohang-turn")
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    if player.chained == false then
      local choices = {
        "doublez__duohang_endTurn",
        "chained"
      }
      local choice = room:askToChoice(player, {
        skill_name = duohang.name,
        choices = choices
      })
      event:setCostData(self, choice)
    else
      room:delay(200)
      event:setCostData(self, "doublez__duohang_endTurn")
    end
    return true
  end,
  on_use = function (self, event, target, player, data)
    local choice = event:getCostData(self)
    if choice == "chained" then
      player:setChainState(true)
    else
      player.room:endTurn()
    end
  end,
  can_refresh = function (self, event, target, player, data)
    return target == player and player:hasSkill(self, true, true) and player.room.current == player and player:usedSkillTimes(duohang.name) > 0
  end,
  on_refresh = reset,
})

return duohang